﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Common;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Admin.Models.WorkTaskManager;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;

namespace Zocono.WCS.Admin.Controllers.MonitorManageMoudle
{

    [BasicAuth]
    public class CurrentWorkTaskInfoController : BaseController
    {
        private readonly WorkTaskInfoApp _workTaskInfoApp;
        private readonly TaskRouteDetailRecordInfoApp _taskRouteDetailRecordInfoApp;
        public CurrentWorkTaskInfoController(TaskRouteDetailRecordInfoApp taskRouteDetailRecordInfoApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor, WorkTaskInfoApp  workTaskInfoApp) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            _workTaskInfoApp= workTaskInfoApp;
            this._taskRouteDetailRecordInfoApp = taskRouteDetailRecordInfoApp;
        }

        public IActionResult Index()
        {
            return View();
        }



        public  ActionResult CurrentWorkTaskInfoAsync()
        {
            var taskSearchData = new WorkTaskInfoSearchData();
            //await AddTracesInfo();

            return View(taskSearchData);
        }


        public async Task<JsonResult> GetCurrentAllWorkTaskInfoAsync()
        {
            var where = new QueryWorkTaskInfoDto();
            where.PageIndex = (Start / Length) + 1;
            where.PageSize = Length;
            //where.FromStation = Request.Form["FromStation"].ToString();
            //where.ToStation = Request.Form["ToStation"].ToString();
            //where.WmsFromStation = Request.Form["WmsFromStation"].ToString();
            //where.WmsToStation = Request.Form["WmsToStation"].ToString();
            //where.WmsFromPosition = Request.Form["WmsFromPosition"].ToString();
            //where.WmsToPosition = Request.Form["WmsToPosition"].ToString();
            where.PalletCode = Request.Form["PalletCode"].ToString();
            //where.TaskState = Request.Form["TaskState"].ToInt32();
            var TaskCode = Request.Form["TaskCode"].ToInt32();
            if (TaskCode != 0)
            {
                where.TaskCode = TaskCode;
            }

            //var WmsTaskCode = Request.Form["WmsTaskCode"].ToInt32();
            //if (WmsTaskCode != 0)
            //{
            //    where.WmsTaskCode = WmsTaskCode;
            //}
                where.RouteInfo = Request.Form["RouteInfo"].ToString();


            var TaskState = Request.Form["TaskState"].ToInt32();
            if (TaskState != -1)
            {
                where.TaskState = TaskState;
            }

            //var TaskProcessState = Request.Form["TaskProcessState"].ToInt32();
            //if (TaskProcessState != -1)
            //{
            //    where.TaskProcessState = TaskProcessState;
            //}

            var workTaskInfo = await _workTaskInfoApp.GetPagedCurrentWorkTaskInfoAsync(where);
            var workTaskPageDatas = new List<WorkTaskInfoPageData>();
            if (workTaskInfo != null && workTaskInfo.Result != null && workTaskInfo.Result.Count > 0)
            {
                foreach (var item in workTaskInfo.Result)
                {
                    workTaskPageDatas.Add(new WorkTaskInfoPageData()
                    {
                        ID = item.ID,
                        RouteID = item.RouteID,
                        FromStation = item.FromStation,
                        ToStation = item.ToStation,
                        Priority = item.Priority,
                        PalletCode = item.PalletCode,
                        TaskCode = item.TaskCode,
                        TaskProcessState = (int)item.TaskProcessState,
                        TaskState = (int)item.TaskState,
                        TaskType = (int)item.TaskType,
                        SaveTime = item.SaveTime,
                        UpdateTime = item.UpdateTime,
                        WmsTaskCode = item.WmsTaskCode,
                        DependWmsTaskCode = item.DependWmsTaskCode,
                        WmsFromStation = item.WmsFromStation,
                        WmsFromPosition = item.WmsFromPosition.ToString(),
                        WmsToStation = item.WmsToStation,
                        WmsToPosition = item.WmsToPosition,
                        CurrentRounteDetailID = item.CurrentRounteDetailID,
                        CurrentDetailTaskState = (int)item.CurrentDetailTaskState,
                        CurrentDispatchType = (int)item.CurrentDispatchType,
                        CurrentFromEquipment = item.CurrentFromEquipment,
                        CurrentFromCargo = item.CurrentFromCargo,
                        CurrentToEquipment = item.CurrentToEquipment,
                        CurrentToCargo = item.CurrentToCargo,
                        CurrentCreateTime = item.CurrentCreateTime,
                        CurrentUpdateTime = item.CurrentUpdateTime,
                        UploadWmsState = (int)item.UploadWmsState,
                        UploadWmsTime = item.UploadWmsTime
                    });
                }
            }
            var pageData = new TableData<WorkTaskInfoPageData>(Draw, workTaskInfo.Total, workTaskInfo.Total, workTaskPageDatas);

            //await AddTracesInfo(5, 0, "获取任务列表信息");
            return Json(pageData);
        }




        public ActionResult CurrentTaskRouteDetailInfoAsync()
        {
            //await AddTracesInfo();
            return View();
        }


        public async Task<JsonResult> GetCurrentTaskDetailInfoByIdAsync(long id)
        {
            var taskInfos = await _taskRouteDetailRecordInfoApp.GetTaskRouteDetailRecordInfoAsync(id);

            var taskPageDatas = new List<TaskRouteDetailInfoPageData>();
            if (taskInfos != null && taskInfos.Count > 0)
            {
                foreach (var item in taskInfos)
                {
                    taskPageDatas.Add(new TaskRouteDetailInfoPageData()
                    {
                        ID = item.ID,
                        TaskID = item.TaskID,
                        RouteDetailID = item.RouteDetailID,
                        DetailTaskState = item.DetailTaskState,
                        DispatchType = item.DispatchType,
                        FromEquipment = item.FromEquipment,
                        FromCargo = item.FromCargo,
                        ToCargo = item.ToCargo,
                        ToEquipment = item.ToEquipment,
                        CreateTime = item.CreateTime,
                        UpdateTime = item.UpdateTime,
                    });
                }
            }
            var pageData = new TableData<TaskRouteDetailInfoPageData>(Draw, taskPageDatas.Count, taskPageDatas.Count, taskPageDatas);

            //await AddTracesInfo(5, 0, "获取task详情");
            return Json(pageData);
        }
    }
}
